﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SecurityDev
{
    public partial class ObjectDetail : Form
    {
        string[] photosStr;
        string[] schemasStr;
        string[] pathsStr;
        string coordinates;

        public ObjectDetail(decimal obj_seq)
        {
            OutpostDS.objectRow objectR = DS.outpost._object.FindByobj_seq(obj_seq);

            InitializeComponent();
            this.Text = objectR.obj_name;

            label1.Text = objectR.obj_seq.ToString();
            label2.Text = objectR.obj_name;
            label3.Text = objectR.obj_phone;
            label4.Text = objectR.obj_adress;
            label5.Text = objectR.obj_category;
            label6.Text = objectR.obj_priority.ToString();
            string[] str = objectR.obj_coord.Split(',');
            if (str.Count() == 2)
            {
                str[0] = str[0].Substring(0, str[0].IndexOf('.') + 6) + ',' + str[1].Substring(0, str[1].IndexOf('.') + 6);
                coorLinkLabel.Text = str[0];
            }
            else
            {
                coorLinkLabel.Text = "";
            }
            
            
            coordinates = objectR.obj_coord; // to open in the browser

            photosStr = objectR.obj_photo_url.Split(';');
            photosListBox.Items.AddRange((object[])
                (from s in photosStr
                 select " " + System.IO.Path.GetFileName(s)).ToArray());

            schemasStr = objectR.obj_schema.Split(';');
            schemasListBox.Items.AddRange((object[])
                (from s in schemasStr
                 select " " + System.IO.Path.GetFileName(s)).ToArray());

            pathsStr = objectR.obj_path.Split(';');
            pathsListBox.Items.AddRange((object[])
                (from s in pathsStr
                 select " " + System.IO.Path.GetFileName(s)).ToArray());
            

            decimal[] perSeq_inObjPer = (from op in DS.outpost.object_person
                                        where op.obj_seq == obj_seq
                                        select op.person_seq).ToArray();

            var perStr = from p in DS.outpost.person
                         where CheckPerSeq_inArray(p.person_seq, perSeq_inObjPer)
                         select string.Format("Id: {0}; Name: {1} {2}", p.person_seq.ToString(), p.person_name, p.person_lastname);

            foreach (var item in perStr)
            {
                personsListBox.Items.Add((object)item);
            }
        }

        bool CheckPerSeq_inArray(decimal perSeq, decimal[] array)
        {
            foreach (var item in array)
            {
                if (perSeq == item)
                {
                    return true;
                }
            }
            return false;
        }

        private void coorLinkLabel_Click(object sender, EventArgs e)
        {
            string[] str = coorLinkLabel.Text.Split(',');
            if (str.Count() == 2 && str[0] != "" && str[1] != "")
            {
                ProcessStartInfo sInfo = new ProcessStartInfo("http://maps.google.com/?q=" + coordinates);
                Process.Start(sInfo);
            }
        }

        private void photoslistBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadImage(pictureBox, photosStr[photosListBox.SelectedIndex], false);
        }

        private void schemasListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadImage(pictureBox, schemasStr[schemasListBox.SelectedIndex], false);
        }

        private void pathsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadImage(pictureBox, pathsStr[pathsListBox.SelectedIndex], false);
        }

        private void ObjectDetail_Load(object sender, EventArgs e)
        {
            if (photosStr[0] != null)
            {
                photosListBox.SelectedIndex = 0;
                LoadImage(pictureBox, photosStr[0], false);
            }
        }

        private void personsListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (personsListBox.SelectedIndex != -1)
            {
                decimal pers_seq = Convert.ToDecimal(((string)personsListBox.SelectedItem).Split(';')[0].Substring(4));
                (new PersonDetail(pers_seq)).ShowDialog();
            }
            personsListBox.ClearSelected();
        }

        private void LoadImage(PictureBox p1, string path, bool type)
        {
            // if (type == true) - open in Browser - else only on Form
            if (type == true)
            {
                ProcessStartInfo sInfo = new ProcessStartInfo(path);
                Process.Start(sInfo);
            }
            try
            {
                p1.Load(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void ObjectDetail_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Escape)
            {
                Close();
            }
        }
    }
}